package com.vipheyue.huaxue.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.vipheyue.huaxue.R;
import com.vipheyue.huaxue.bean.MyUser;
import com.vipheyue.huaxue.bean.ThingBean;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

public class ReleaseActivity extends AppCompatActivity {

    @Bind(R.id.iv_Menu_leftImg)
    ImageView iv_Menu_leftImg;
    @Bind(R.id.tv_Menu_leftTitle)
    TextView tv_Menu_leftTitle;
    @Bind(R.id.rl_menu_left)
    RelativeLayout rl_menu_left;
    @Bind(R.id.tv_Menu_Title)
    TextView tv_Menu_Title;
    @Bind(R.id.iv_menu_rightTitle)
    TextView iv_menu_rightTitle;
    @Bind(R.id.iv_menu_right)
    ImageView iv_menu_right;
    @Bind(R.id.rl_menu_right)
    RelativeLayout rl_menu_right;
    @Bind(R.id.et_release_name)
    EditText et_release_name;
    @Bind(R.id.et_release_weight)
    EditText et_release_weight;
    @Bind(R.id.et_release_purity)
    EditText et_release_purity;
    @Bind(R.id.et_release_Deliverydate)
    EditText et_release_Deliverydate;
    @Bind(R.id.et_release_structure)
    EditText et_release_structure;
    @Bind(R.id.btn_release_commit)
    Button btn_release_commit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release);
        ButterKnife.bind(this);
        tv_Menu_Title.setText("发布");
    }

    private Boolean checkStr(EditText editText) {
        if (TextUtils.isEmpty(editText.getText().toString().trim())) {
            return true;//不为空
        }
        return false;
    }

    @OnClick({R.id.rl_menu_left, R.id.btn_release_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_menu_left:
                finish();
                break;
            case R.id.btn_release_commit:
                if (checkStr(et_release_name) || checkStr(et_release_Deliverydate) || checkStr(et_release_purity) || checkStr(et_release_structure) || checkStr(et_release_weight)) {
                    Toast.makeText(this, "请填写各项数据", Toast.LENGTH_SHORT).show();
                    return;
                }

                ThingBean thingBean = new ThingBean();
                MyUser user = BmobUser.getCurrentUser(this, MyUser.class);
                thingBean.setReleaseUser(user);
                thingBean.setDeliverydate(et_release_Deliverydate.getText().toString().trim());
                thingBean.setName(et_release_name.getText().toString().trim());
                thingBean.setPurity(et_release_purity.getText().toString().trim());
                thingBean.setStructure(et_release_structure.getText().toString().trim());
                thingBean.setWeight(et_release_weight.getText().toString().trim());
                    thingBean.save(this, new SaveListener() {
                        @Override
                        public void onSuccess() {
                            Toast.makeText(ReleaseActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            Toast.makeText(ReleaseActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                break;
        }
    }
}
